package com.hexinfo.dmpro.monitor.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.common.base.Page;
import com.hexinfo.dmpro.monitor.model.serviceparam.*;
import com.hexinfo.dmpro.monitor.service.serviceparam.CmTargetParamService;
import com.hexinfo.dmpro.monitor.service.serviceparam.DingDingAlarmService;
import com.hexinfo.dmpro.monitor.service.serviceparam.GiveAnAlarmService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 指标参数
 * @author peng
 * @date 2022/12/7 14:09
 */

@RestController
@RequestMapping("/monitor_api/cmTarget")
public class CmTargetController {


    @Autowired
    private CmTargetParamService cmTargetParamService;
    @Autowired
    private GiveAnAlarmService giveAnAlarmService;
    @Autowired
    private DingDingAlarmService dingDingAlarmService;

    /**
     * 查询集群参数配置 首页
     * @param page
     * @param req
     * @return
     */
    @GetMapping(value = "/selClusterService")
    public ResponseEntity<Object> selClusterService(Page page, @Param("int") int current, CmServiceParamReq req) {
        page.setPageIndex(current-1);
        return new ResponseEntity<>(cmTargetParamService.selClusterService(page, req), HttpStatus.OK);
    }

    /**
     * 参数管理-关键指标-组件栏
     * @param req
     * @return
     */
    @GetMapping(value = "/selClusterServiceType")
    public ResponseEntity<Object> selClusterServiceType(CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.selClusterServiceType(req), HttpStatus.OK);
    }

    /**
     * 查询集群参数告警信息
     * @param page
     * @param req
     * @return
     */
    @GetMapping(value = "/selTargetAlarmInfo")
    public ResponseEntity<Object> selTargetAlarmInfo(Page page, @Param("int") int current, CmServiceParamReq req) {
        page.setPageIndex(current-1);
        return new ResponseEntity<>(cmTargetParamService.selTargetAlarmInfo(page, req), HttpStatus.OK);
    }


    /**
     * 查询集群指标配置信息
     * @param req
     * @return
     */
    @GetMapping(value = "/selClusterServiceTarget")
    public ResponseEntity<Object> selClusterServiceTarget(CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.selClusterServiceTarget(req), HttpStatus.OK);
    }

    /**
     * 添加集群指标配置信息
     * @param req
     * @return
     */
    @PostMapping(value = "/saveClusterServiceTarget")
    public ResponseEntity<Object> saveClusterServiceTarget(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.saveClusterServiceTarget(req), HttpStatus.OK);
    }


    /**
     * 处理警告信息
     * @param req
     * @return
     */
    @PostMapping(value = "/upTargetAlarmInfo")
    public ResponseEntity<Object> upTargetAlarmInfo(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.upTargetAlarmInfo(req), HttpStatus.OK);
    }

    /**
     * 查询指标明细信息 前端图形  获取集群某个服务下所有指标编码
     * @param req
     * @return
     */
    @PostMapping(value = "/selTargetDetailList")
    @AnonymousAccess
    public ResponseEntity<Object> selTargetDetailList(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.selTargetDetailList(req), HttpStatus.OK);
    }

    /**
     * 查询指标名称按最大值排序
     * @param req
     * @return
     */
    @PostMapping(value = "/getTopNameList")
    @AnonymousAccess
    public ResponseEntity<Object> getTopNameList(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.getTopNameList(req), HttpStatus.OK);
    }

    /**
     * 查询指标明细信息 前端图形  公共使用方法
     * @param req
     * @return
     */
    @PostMapping(value = "/selTargetDetail")
    @AnonymousAccess
    public ResponseEntity<Object> selTargetDetail(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.selTargetDetail(req), HttpStatus.OK);
    }

    /**
     * 查询指标名称按最大值排序
     * @param req
     * @return
     */
    @PostMapping(value = "/getTopName")
    @AnonymousAccess
    public ResponseEntity<Object> getTopName(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.getTopName(req), HttpStatus.OK);
    }

    /**
     * 添加容量参数配置信息
     * @param req
     * @return
     */
    @PostMapping(value = "/insMonitorTargetConfig")
    public ResponseEntity<Object> insMonitorTargetConfig(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.insMonitorTargetConfig(req), HttpStatus.OK);
    }

    /**
     * 删除容量参数配置信息
     * @param req
     * @return
     */
    @PostMapping(value = "/delMonitorTargetConfig")
    public ResponseEntity<Object> delMonitorTargetConfig(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.delMonitorTargetConfig(req), HttpStatus.OK);
    }

    /**
     * 修改容量参数配置信息
     * @param req
     * @return
     */
    @PostMapping(value = "/upMonitorTargetConfig")
    public ResponseEntity<Object> upMonitorTargetConfig(@RequestBody CmServiceParamReq req) {
        return new ResponseEntity<>(cmTargetParamService.upMonitorTargetConfig(req), HttpStatus.OK);
    }

    /**
     * 查询容量参数配置信息
     * @param req
     * @return
     */
    @GetMapping(value = "/selMonitorTargetConfig")
    public ResponseEntity<Object> selMonitorTargetConfig(Page page, @Param("int") int current, CmServiceParamReq req) {
        page.setPageIndex(current-1);
        return new ResponseEntity<>(cmTargetParamService.selMonitorTargetConfig(page,req), HttpStatus.OK);
    }

    /**
     * 微信、钉钉告警，然后添加进告警日志表
     * @param jsonData
     * @return
     */
    @PostMapping(value = "/warnWxDingDingAndLog")
    @AnonymousAccess
    public void warnWxDingDingAndLog(@RequestParam String jsonData) {
        JSONObject jsonObject = JSONUtil.parseObj(jsonData);
        Map<String, String> map = jsonObject.toBean(Map.class);
        cmTargetParamService.warnWxDingDingAndLog(map.get("cmFlag"),map.get("cmName"),map.get("clusterFlag"),
                map.get("clusterName"), map.get("serviceName"),map.get("warnAlert"),map.get("publishTheme"));
    }

    /**
     * 告警信息平台（微信）
     * @param wx
     * @return
     */
    @PostMapping(value = "/giveAnAlarmWX")
    public void giveAnAlarmWX(@RequestBody WXGiveAnAlarmModel wx) {
        giveAnAlarmService.giveAnAlarmWX(wx);
    }

    /**
     * 钉钉群聊告警（手机号）
     * @param dingDingAlarmModel
     * @return
     */
    @PostMapping(value = "/dingDingAlarm")
    public void dingDingAlarm(@RequestBody DingDingAlarmModel dingDingAlarmModel) {
        dingDingAlarmService.dingDingAlarm(dingDingAlarmModel.getContent(),
                dingDingAlarmModel.getMobileList(),dingDingAlarmModel.getIsAtAll());
    }

    /**
     * 获取容量参数设置服务类型
     * @return
     */
    @GetMapping(value = "/selServiceType")
    public ResponseEntity<Object> selServiceType() {
        return new ResponseEntity<>(cmTargetParamService.selServiceType(), HttpStatus.OK);
    }

    /**
     * 按服务类型查询容量参数设置
     * @return
     */
    @GetMapping(value = "/selTypeTargetConfig")
    public ResponseEntity<Object> selTypeTargetConfig(String serviceType) {
        return new ResponseEntity<>(cmTargetParamService.selTypeTargetConfig(serviceType), HttpStatus.OK);
    }

    /**
     * 按指标名称查询告警用户信息
     * @return
     */
    @GetMapping(value = "/selTypeWarningUser")
    public ResponseEntity<Object> selTypeWarningUser(String targetName) {
        return new ResponseEntity<>(cmTargetParamService.selTypeWarningUser(targetName), HttpStatus.OK);
    }

    /**
     * 获取接收人-列表
     * @return
     */
    @GetMapping(value = "/selWarningUserList")
    public ResponseEntity<Object> selWarningUserList(Page page,@Param("int") int current, WarningUserModel user) {
        page.setPageIndex(current-1);
        return new ResponseEntity<>(cmTargetParamService.selWarningUserList(page,user), HttpStatus.OK);
    }

    /**
     * 获取接收人
     * @return
     */
    @GetMapping(value = "/selWarningUser")
    public ResponseEntity<Object> selWarningUser() {
        return new ResponseEntity<>(cmTargetParamService.selWarningUser(), HttpStatus.OK);
    }

    /**
     * 添加接收人
     * @return
     */
    @PostMapping(value = "/addWarningUser")
    public ResponseEntity<Object> addWarningUser(@RequestBody WarningUserModel warn) {
        return new ResponseEntity<>(cmTargetParamService.addWarningUser(warn), HttpStatus.OK);
    }

    /**
     * 修改接收人
     * @return
     */
    @PostMapping(value = "/updateWarningUser")
    public ResponseEntity<Object> updateWarningUser(@RequestBody WarningUserModel warn) {
        return new ResponseEntity<>(cmTargetParamService.updateWarningUser(warn), HttpStatus.OK);
    }

    /**
     * 删除接收人
     * @return
     */
    @GetMapping(value = "/deleteWarningUser")
    public ResponseEntity<Object> deleteWarningUser(String id) {
        return new ResponseEntity<>(cmTargetParamService.deleteWarningUser(id), HttpStatus.OK);
    }

    /**
     * 参数管理-告警详情-告警发送-添加
     * @return
     */
    @PostMapping(value = "/addWarnSendConfig")
    public ResponseEntity<Object> addWarnSendConfig(@RequestBody WarnSendConfigModel warn) {
        return new ResponseEntity<>(cmTargetParamService.addWarnSendConfig(warn), HttpStatus.OK);
    }

    /**
     * 参数管理-告警详情-告警发送-修改
     * @return
     */
    @PostMapping(value = "/updateWarnSendConfig")
    public ResponseEntity<Object> updateWarnSendConfig(@RequestBody WarnSendConfigModel warn) {
        return new ResponseEntity<>(cmTargetParamService.updateWarnSendConfig(warn), HttpStatus.OK);
    }

    /**
     * 参数管理-告警详情-告警发送-删除
     * @return
     */
    @GetMapping(value = "/deleteWarnSendConfig")
    public ResponseEntity<Object> deleteWarnSendConfig(String id) {
        return new ResponseEntity<>(cmTargetParamService.deleteWarnSendConfig(id), HttpStatus.OK);
    }

    /**
     * 参数管理-告警详情-告警发送-列表
     * @return
     */
    @GetMapping(value = "/selWarnSendConfig")
    public ResponseEntity<Object> selWarnSendConfig(Page page, @Param("int") int current, WarnSendConfigModel warn) {
        page.setPageIndex(current-1);
        return new ResponseEntity<>(cmTargetParamService.selWarnSendConfig(page,warn), HttpStatus.OK);
    }

    /**
     * 参数管理-告警详情-告警发送-发布
     * @return
     */
    @GetMapping(value = "/releaseWarnSendConfig")
    public ResponseEntity<Object> releaseWarnSendConfig(String id) {
        return new ResponseEntity<>(cmTargetParamService.releaseWarnSendConfig(id), HttpStatus.OK);
    }

    /**
     * 参数管理-告警详情-告警发送-待发布
     * @return
     */
    @GetMapping(value = "/noReleaseWarnSendConfig")
    public ResponseEntity<Object> noReleaseWarnSendConfig(String id) {
        return new ResponseEntity<>(cmTargetParamService.noReleaseWarnSendConfig(id), HttpStatus.OK);
    }

    /**
     * 告警日志表分页
     * @return
     */
    @GetMapping(value = "/selWarnSendLog")
    public ResponseEntity<Object> selWarnSendLog(Page page,@Param("int") int current, WarnSendLogModel warn) {
        page.setPageIndex(current-1);
        return new ResponseEntity<>(cmTargetParamService.selWarnSendLog(page,warn), HttpStatus.OK);
    }

    /**
     * 日常巡检
     * @return
     */
    @GetMapping(value = "/seleveryDayInspect")
    @AnonymousAccess
    public ResponseEntity<Object> seleveryDayInspect() {
        return new ResponseEntity<>(cmTargetParamService.seleveryDayInspect(), HttpStatus.OK);
    }

    /**
     * CM服务监控
     * @return
     */
    @GetMapping(value = "/cmServiceWarn")
    @AnonymousAccess
    public ResponseEntity<Object> cmServiceWarn() {
        return new ResponseEntity<>(cmTargetParamService.cmServiceWarn(), HttpStatus.OK);
    }

    /**
     * 发送告警
     * @return
     */
    @PostMapping(value = "/sendWarn")
    @AnonymousAccess
    public void sendWarn(@RequestBody Map<String,String> jsonMap) {
        cmTargetParamService.warnWxDingDingAndLog1("cmFlag","所有集群","clusterFlag","所有子集群","所有服务",jsonMap.get("warnAlert"),jsonMap.get("publishTheme"));
    }
}
